package com.game.battleship;

import com.game.battleship.db.DbAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PlayerLoginActivity extends Activity {
	
	Context context;
	
	DbAdapter dbAdapter;
	
	EditText loginPlayerName_EditText;
	EditText loginPassword_EditText;
	
	Button loginAndContinue_Button;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player_login);
        
        context = this.getApplicationContext();
        
        dbAdapter = new DbAdapter(context);
        
        loginPlayerName_EditText = (EditText)this.findViewById(R.id.loginPlayerName_EditText);
        loginPassword_EditText = (EditText)this.findViewById(R.id.loginPassword_EditText);
        
        loginAndContinue_Button = (Button)this.findViewById(R.id.loginAndContinue_Button);
        loginAndContinue_Button.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String username = loginPlayerName_EditText.getText().toString();
				String password = loginPassword_EditText.getText().toString();
				boolean validationSucceeded = validateEditTextInputString(username) & validateEditTextInputString(password);
				if (validationSucceeded) {
					// do something with the database
					boolean authSucceeded = false;
					try {
						authSucceeded = dbAdapter.authenticate(username, password);
						if (authSucceeded) {
							Toast.makeText(context, "Welcome back, " + username + "!", Toast.LENGTH_SHORT).show();
							// start the next activity
							Intent intent = new Intent(PlayerLoginActivity.this, UserInfoActivity.class);
							intent.putExtra(UserInfoActivity.EXT_USERNAME, username);
							startActivity(intent);
						}
						else {
							Toast.makeText(context, "The username or password is not correct!", Toast.LENGTH_SHORT).show();
						}
					}
					catch (SQLException ex) {
						Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
				else {
					Toast.makeText(context, "Invalid input data! The input only accepts alphanumeric characters.", Toast.LENGTH_SHORT).show();
				}				
			}
        	
        });
	}

	private boolean validateEditTextInputString(String str) {
		// TODO Auto-generated method stub		
		if (isValidIdentifier(str)) {
			return true;
		}
		return false;
	}

	private boolean isValidIdentifier(String str) {
		// TODO Auto-generated method stub
		if (str == null) return false;
		int len = str.length();
		if (len == 0) return false;
		for (int i=0;i<len;++i) {
			char c = str.charAt(i);			
			if (!Character.isLetter(c) && !Character.isDigit(c)) {
				return false;
			}
		}
		return true;
	}
}
